package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ScoreResult;
import com.kbbywyclient.data.model.ScoreInfoModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.ScoreDetailContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/5/8
 */

public class ScoreDetailPresenter extends BasePresenter implements ScoreDetailContract.Presenter {

    private ScoreDetailContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private int pageno = 1;
    private List<ScoreResult> scoreResultList = null;

    @Inject
    public ScoreDetailPresenter(ScoreDetailContract.View view) {
        this.view = view;
        scoreResultList = new ArrayList<>();
        this.view.setAdapterOrNotify(scoreResultList);
    }

    @Override
    public void getScoreList(boolean isClean, String SalesID, String ScoreType) {
        if (isClean) {
            pageno = 1;
        }
        rsaJsonUtil.put("pageno", pageno + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);
        rsaJsonUtil.put("SalesID", SalesID);
        if (StringUtil.isNotEmpty(ScoreType)) {
            rsaJsonUtil.put("ScoreType", ScoreType);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getScoreList(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<ScoreInfoModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.loadFaiScore(e.toString());
                    }

                    @Override
                    public void onNext(ScoreInfoModel scoreInfoModel) {
                        if (isClean) {
                            scoreResultList.clear();
                        }
                        scoreResultList.addAll(scoreInfoModel.getData().getResult());
                        view.setAdapterOrNotify(scoreResultList);//刷新
                        view.loadSucScore(scoreResultList);
                        //分页
                        if (scoreInfoModel.getData().getPaging().getCurrentPage() >= scoreInfoModel.getData().getPaging().getTotalPage()) {
                            if (scoreInfoModel.getData().getResult().size() > 1) {
                                view.setNoMore();
                            }
                        } else {
                            pageno++;
                            view.setMoreComplete();
                        }
                    }
                });

    }


}
